import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/model/pay_model/user_list_pay_certification_model.dart';
import 'package:yysk_app/pages/main/main_page.dart';
import 'package:yysk_app/pages/nft_pages/setting/setting_real_name_auth_page.dart';
import 'package:yysk_app/pages/nft_pages/wallet_pages/settle_detail/settle_detail_page.dart';
import 'package:yysk_app/pages/nft_pages/wallet_pages/wallet_personal/wallet_personal_page.dart';
import 'package:yysk_app/routes/global_controller.dart';
import 'package:yysk_app/utils/colors.dart';
import 'package:yysk_app/utils/dialog_widget.dart';
import 'package:yysk_app/utils/helper.dart';
import 'package:yysk_app/widget/button_widget.dart';
import 'package:yysk_app/widget/custom_app_bar2.dart';
import 'package:yysk_app/widget/image_asset_widget.dart';
import 'package:yysk_app/widget/lazy_network_image.dart';
import 'wallet_controller.dart';

class WalletPage extends GetView<WalletController> {
  static const String path = '/wallet';
  const WalletPage({super.key});

  @override
  Widget build(BuildContext context) {
    double top = MediaQuery.of(context).padding.top;
    GlobalController globalController = Get.find();
    return Scaffold(
      body: Stack(
        children: [
          Positioned(
            top: 0,
            left: 0,
            right: 0,
            child: ImageAssetWidget(
              width: 375.px,
              height: 812.px,
              imageUrl: 'static/images/bg.png',
            ),
          ),
          Padding(
            padding: EdgeInsets.fromLTRB(0, top, 0, 24.px),
            child: Obx(
              () => Column(
                children: [
                  CustomAppBar2(title: '我的钱包', context: context),
                  Expanded(
                    child: RefreshIndicator(
                      backgroundColor: Colors.transparent,
                      color: Colors.white,
                      onRefresh: controller.handleRefresh,
                      child: _buildWalletList(context, globalController),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildWalletList(
    BuildContext context,
    GlobalController globalController,
  ) {
    return SingleChildScrollView(
      physics: AlwaysScrollableScrollPhysics(),
      child: Padding(
        padding: EdgeInsets.all(12.px),
        child: Column(
          children: [
            // if (controller.bankList.isEmpty) EmptyWidget(),
            Container(
              width: double.infinity,
              margin: EdgeInsets.only(bottom: 12.px),
              padding: EdgeInsets.fromLTRB(16.px, 14.px, 16.px, 20.px),
              decoration: BoxDecoration(
                color: AppColors.backgroundColorBlackLight,
                borderRadius: BorderRadius.circular(10.px),
                border: Border.all(
                  width: 1.px,
                  color: Color.fromRGBO(255, 255, 255, 0.10),
                ),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    '可用余额（元）',
                    style: TextStyle(
                      fontSize: 16.px,
                      color: AppColors.fontColorGray99,
                    ),
                  ),
                  SizedBox(height: 18.px),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        '${controller.hasWallet.value}',
                        style: TextStyle(
                          fontSize: 24.px,
                          fontWeight: FontWeight.w500,
                          color: AppColors.fontColor,
                        ),
                      ),
                      ButtonWidget(
                        label: '余额管理',
                        height: 27.px,
                        width: 74.px,
                        fontSize: 12.px,
                        fontColor: AppColors.fontColorBlack,
                        colorStart: AppColors.fontColorStart00,
                        colorEnd: AppColors.fontColorEnd00,
                        borderRadius: 6,
                        onPressed: () {
                          Get.toNamed(
                            WalletPersonalPage.path,
                            arguments: {'channel': 'DIN_PAY'},
                          );
                        },
                      ),
                    ],
                  ),
                ],
              ),
            ),

            ...controller.listPayCertification.map((e) {
              return _walletCard(
                item: e!,
                context,
                bgImage: 'static/images/wallet/wallet_card_1.png',
                amount: '${e.balance}',
                type: '智付支付',
                buttonText: '开通钱包',
                onPressed: () {
                  //实名认证逻辑暂时注释
                  if (globalController.hasAuth.value == false) {
                    toOpenWallet(context: context);
                    return;
                  }
                  controller.plat = e.plat.toString();
                  controller.channel = e.channel.toString();
                  if (e.plat == 'USER') {
                    if (e.isCertified == true &&
                        e.certificationStatus == 'PASS') {
                      if (e.channel == 'DIN_PAY') {
                        //跳转智付钱包个人版页面
                        controller.getWalletUrl(
                          e.plat.toString(),
                          e.channel.toString(),
                        );
                      }
                      // else {
                      //   //跳转汇付钱包个人版页面
                      //   Get.toNamed(
                      //     WalletPersonalPage.path,
                      //     arguments: {'channel': e.channel},
                      //   );
                      // }
                      return;
                    } else {
                      controller.submit();
                      return;
                    }
                  }
                },
              );
            }),
          ],
        ),
      ),
    );
  }

  //去实名认证
  toOpenWallet({required BuildContext context}) async {
    await showConfirmDialog(
      context: context,
      message: '请先完成实名认证',
      messages: [],
      fontSize: 16,
      actions: [
        ActionModel(
          label: '取消',
          colorStart: AppColors.backgroundColorGreyF8,
          colorEnd: AppColors.backgroundColorGreyF8,
          fontColor: AppColors.fontColor,
          onPressed: () {},
        ),
        ActionModel(
          label: '立即认证',
          onPressed: () {
            try {
              Get.until((route) => Get.currentRoute == MainPage.path);
            } catch (e) {
              Get.offAllNamed(MainPage.path);
            }
            Get.toNamed(SettingRealNameAuthPage.path);
          },
        ),
      ],
    );
  }

  Widget _walletCard(
    BuildContext context, {
    required String bgImage,
    required String amount,
    required String type,
    required String buttonText,
    required VoidCallback onPressed,
    required UserListPayCertificationModel item,
  }) {
    return Column(
      children: [
        Container(
          width: double.infinity,
          margin: EdgeInsets.only(bottom: 12.px),
          padding: EdgeInsets.fromLTRB(16.px, 14.px, 16.px, 20.px),
          decoration: BoxDecoration(
            color: AppColors.backgroundColorBlackLight,
            borderRadius: BorderRadius.circular(10.px),
            border: Border.all(
              width: 1.px,
              color: Color.fromRGBO(255, 255, 255, 0.10),
            ),
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                type == '智付支付' ? 'ZHIFUPAY' : '',
                style: TextStyle(
                  fontSize: 16.px,
                  color: AppColors.fontColorGray99,
                ),
              ),
              SizedBox(height: 18.px),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Row(
                    children: [
                      LazyImage(
                        imageUrl: '${item.iconUrl}',
                        width: item.channel == "DEBIT_CARD" ? 32.px : 32.px,
                        height: 32.px,
                        borderRadius: 32,
                      ),
                      SizedBox(width: 12.px),
                      Text(
                        type,
                        style: TextStyle(
                          fontSize: 16.px,
                          color: AppColors.fontColor,
                        ),
                      ),
                    ],
                  ),
                  // Text(
                  //   '￥ $amount',
                  //   style: TextStyle(
                  //     fontSize: 24.px,
                  //     fontWeight: FontWeight.w500,
                  //     color: AppColors.fontColor,
                  //   ),
                  // ),
                  Row(
                    children: [
                      item.isCertified == true &&
                              item.certificationStatus == 'WAITING'
                          ? SizedBox()
                          : ButtonWidget(
                            label: '结算明细',
                            height: 27.px,
                            width: 74.px,
                            fontSize: 12.px,
                            fontColor: AppColors.fontColorSearch,
                            bgColorStart: AppColors.backgroundColorBlackLight,
                            bgColorEnd: AppColors.backgroundColorBlackLight,
                            borderRadius: 6,
                            border: Border.all(
                              width: 1.px,
                              color: AppColors.fontColorSearch,
                            ),
                            onPressed: () {
                              Get.toNamed(
                                '${SettleDetailPage.path}?type=1&channel=DIN_PAY',
                              );
                            },
                          ),
                      hiSpace(width: 12.px),
                      ButtonWidget(
                        label:
                            (item.isCertified == true &&
                                    item.certificationStatus == 'PASS')
                                ? '钱包管理'
                                : ((item.isCertified == true &&
                                        item.certificationStatus == 'WAITING')
                                    ? '待审核'
                                    : '开通钱包'),
                        height: 27.px,
                        width: 74.px,
                        fontSize: 12.px,
                        fontColor: AppColors.fontColorBlack,
                        borderRadius: 6,
                        onPressed: onPressed,
                      ),
                    ],
                  ),
                ],
              ),
              // SizedBox(height: 18.px),
              // Container(
              //   padding: EdgeInsets.all(10),
              //   width: double.infinity,
              //   decoration: BoxDecoration(
              //     borderRadius: BorderRadius.circular(10.px),
              //     color: AppColors.backgroundColorUltimateLightGreen,
              //   ),
              //   child: Row(
              //     mainAxisAlignment: MainAxisAlignment.spaceBetween,
              //     children: [
              //       Row(
              //         children: [
              //           // ImageAssetWidget(
              //           //   imageUrl:
              //           //       'static/images/wallet/newpay_logo.png',
              //           //   width: 30.px,
              //           //   height: 17.px,
              //           // ),
              //           LazyImage(
              //             imageUrl: '${item.iconUrl}',
              //             width: item.channel == "DEBIT_CARD" ? 30.px : 17.px,
              //             height: 17.px,
              //           ),
              //           SizedBox(width: 8.px),
              //           Text(
              //             type,
              //             style: TextStyle(
              //               fontSize: 12.px,
              //               color: AppColors.fontColorBlack,
              //             ),
              //           ),
              //         ],
              //       ),
              //     ],
              //   ),
              // ),
            ],
          ),
        ),
        // (item.isCertified == true && item.certificationStatus == 'PASS')
        //     ? Text('')
        //     : Column(
        //       children: [
        //         SizedBox(height: 16.px),
        //         Text(
        //           '您尚未开通该钱包，或影响资产到账，请及时开通',
        //           style: TextStyle(
        //             fontSize: 12.px,
        //             color: AppColors.fontColorBlack,
        //           ),
        //         ),
        //         SizedBox(height: 16.px),
        //       ],
        //     ),
      ],
    );
  }
}
